// 重新封装 web-storage-cache 库
// web-storage-cache 是对浏览器缓存 localStorage 控制

import Storage from 'web-storage-cache'

const loaclStorage = new Storage()

// 将新数据写入 localStorage
export function setLocalStorage (key, value) {
  return loaclStorage.set(key, value)
}

// 从 localStorage 获取数据
export function getLocalStorage (key) {
  return loaclStorage.get(key)
}

// 从 localStorage 删除数据
export function removeLocalStorage (key) {
  return loaclStorage.delete(key)
}

// 从 localStorage 删除所有数据
export function clearLocalStorage () {
  return loaclStorage.clear()
}

// 存储书籍缓存
export function setBookObject (fileName, key, value) {
  let book = getLocalStorage(`${fileName}-info`) // 从缓存中获取数据
  // 如果缓存中有对应的数据，就将新的内容赋值
  if (!book) {
    book = {}
  }
  book[key] = value
  setLocalStorage(`${fileName}-info`, book)
}

// 获取书籍缓存
export function getBookObject (fileName, key) {
  let book = getLocalStorage(`${fileName}-info`) // 从缓存中获取数据
  if (book) {
    return book[key]
  } else {
    return null
  }
}

// 获取对应书籍的字体
export function getFontFamily (fileName) {
  return getBookObject(fileName, 'fontFamily')
}
// 存储对应书籍的字体
export function saveFontFamily (fileName, font) {
  return setBookObject(fileName, 'fontFamily', font)
}

// 获取阅读主题
export function getTheme (fileName) {
  return getBookObject(fileName, 'theme')
}
// 存储阅读主题
export function saveTheme (fileName, theme) {
  return setBookObject(fileName, 'theme', theme)
}

// 获取字号
export function getFontSize (fileName) {
  return getBookObject(fileName, 'fontSize')
}
// 存储字号
export function saveFontSize (fileName, fontSize) {
  return setBookObject(fileName, 'fontSize', fontSize)
}

export function getHome () {
  return getLocalStorage('home')
}

export function saveHome (home) {
  return setLocalStorage('home', home, 1800)
}

// 获取全局变量缓存
export function getLocale () {
  return getLocalStorage('locale')
}

// 存储全局变量
export function saveLocale (locale) {
  return setLocalStorage('locale', locale)
}

export function getLocation (fileName) {
  return getBookObject(fileName, 'location')
}

export function saveLocation (fileName, location) {
  setBookObject(fileName, 'location', location)
}

export function getBookmark (fileName) {
  return getBookObject(fileName, 'bookmark')
}

export function saveBookmark (fileName, bookmark) {
  setBookObject(fileName, 'bookmark', bookmark)
}

// 获取阅读时间
export function getReadTime (fileName) {
  return getBookObject(fileName, 'time')
}

// 存储阅读时间
export function saveReadTime (fileName, theme) {
  setBookObject(fileName, 'time', theme)
}

export function getProgress (fileName) {
  return getBookObject(fileName, 'progress')
}

export function saveProgress (fileName, progress) {
  setBookObject(fileName, 'progress', progress)
}

export function getNavigation (fileName) {
  return getBookObject(fileName, 'navigation')
}

export function saveNavigation (fileName, navigation) {
  setBookObject(fileName, 'navigation', navigation)
}

export function getMetadata (fileName) {
  return getBookObject(fileName, 'metadata')
}

export function saveMetadata (fileName, metadata) {
  setBookObject(fileName, 'metadata', metadata)
}

export function getCover (fileName) {
  return getBookObject(fileName, 'cover')
}

export function saveCover (fileName, cover) {
  setBookObject(fileName, 'cover', cover)
}
